package com.ruoyi.base.controller;

import com.ruoyi.base.domain.School;
import com.ruoyi.base.service.ISchoolService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学校档案Controller
 *
 * @author ruoyi
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/base/school")
@Api(tags = "学校档案")
public class SchoolController extends BaseController {
    @Autowired
    private ISchoolService schoolService;

    /**
     * 查询学校档案列表
     */
    @GetMapping("/list")
    @ApiOperation("查询学校档案列表")
    public TableDataInfo<School> list(School school) {
        startPage();
        List<School> list = schoolService.selectSchoolList(school);
        return getDataTable(list);
    }

    /**
     * 导出学校档案列表
     */
    @Log(title = "学校档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学校档案列表")
    public void export(HttpServletResponse response, School school) {
        List<School> list = schoolService.selectSchoolList(school);
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.exportExcel(response, list, "学校档案数据");
    }

    /**
     * 获取学校档案详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取学校档案详细信息")
    public R<School> getInfo(@PathVariable("id") String id) {
        return R.ok(schoolService.selectSchoolById(id));
    }

    /**
     * 新增学校档案
     */
    @PreAuthorize("@ss.hasPermi('base:school:add')")
    @Log(title = "学校档案", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增学校档案")
    public AjaxResult add(@RequestBody School school) {
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校档案
     */
    @PreAuthorize("@ss.hasPermi('base:school:edit')")
    @Log(title = "学校档案", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改学校档案")
    public AjaxResult edit(@RequestBody School school) {
        return toAjax(schoolService.updateSchool(school));
    }

    /**
     * 删除学校档案
     */
    @PreAuthorize("@ss.hasPermi('base:school:remove')")
    @Log(title = "学校档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除学校档案")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(schoolService.deleteSchoolByIds(ids));
    }
}
